﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace Freshome.d
{
    public class Access
    {
    }
    // 主料
    public sealed class MajorawDog : b.Accesshort<b.Majoraw>
    {
        public MajorawDog()
        { }
        public List<b.Majoraw> GetMajoraws(bool usedOnly)
        {
            if (usedOnly)
            {
                return p_db.Set<b.Majoraw>().Where(j => j.Recipes.Count > 0).ToList();
            }
            else return p_db.Set<b.Majoraw>().ToList();
        }
    }
    // 佐料
    public sealed class MinorawDog : b.Accesshort<b.Minoraw>
    {
        public MinorawDog()
        { }
        public List<b.Minoraw> GetMinoraws(bool usedOnly)
        {
            if (usedOnly)
            {
                return p_db.Set<b.Minoraw>().Where(i => i.Recipes.Count > 0).ToList();
            }
            else return p_db.Set<b.Minoraw>().ToList();
        }
    }
    // 菜谱
    public sealed class RecipeDog : b.Accesshort<b.Recipe>
    {
        public RecipeDog()
        { }
        public b.Recipe GetRecipe(short mid, bool withCousin)
        {
            var rp = p_db.Set<b.Recipe>().Include(r => r.Steps).Include(r => r.Majors).Include(r => r.Minors)
                .Where(r => r.Mid == mid).AsNoTracking().FirstOrDefault();
            if (rp != null && withCousin)
            {
                var mis = p_db.Set<b.Minoraw>().ToArray();
                var mjs = p_db.Set<b.Majoraw>().ToArray();
                foreach (var ir in rp.Minors)
                {
                    var mi = mis.FirstOrDefault(i => i.Mid == ir.Imx);
                    if (mi != null) ir.Parentitle = mi.Name;
                }
                foreach (var jr in rp.Majors)
                {
                    var mj = mjs.FirstOrDefault(j => j.Mid == jr.Jmx);
                    if (mj != null) jr.Parentitle = mj.Name;
                }
            }
            return rp;
        }
        public List<b.Recipe> GetRecipes(b.RecipeCateg ct, bool withChildren)
        {
            if (ct == b.RecipeCateg.None)
            {
                if (withChildren)
                {
                    var rps = p_db.Set<b.Recipe>().Include(r => r.Steps).Include(r => r.Majors).Include(r => r.Minors).AsNoTracking();
                    var mis = p_db.Set<b.Minoraw>().ToArray();
                    var mjs = p_db.Set<b.Majoraw>().ToArray();
                    foreach (var rp in rps)
                    {
                        foreach (var ir in rp.Minors)
                        {
                            var mi = mis.FirstOrDefault(i => i.Mid == ir.Imx);
                            if (mi != null) ir.Parentitle = mi.Name;
                        }
                        foreach (var jr in rp.Majors)
                        {
                            var mj = mjs.FirstOrDefault(j => j.Mid == jr.Jmx);
                            if (mj != null) jr.Parentitle = mj.Name;
                        }
                    }
                    return rps.ToList();
                }
                else return p_db.Set<b.Recipe>().Include(r => r.Steps).AsNoTracking().ToList();
            }
            else
            {
                if (withChildren)
                {
                    var rps = p_db.Set<b.Recipe>().Include(r => r.Steps).Include(r => r.Majors).Include(r => r.Minors).Where(r => r.Categ == (byte)ct).AsNoTracking();
                    var mis = p_db.Set<b.Minoraw>().ToArray();
                    var mjs = p_db.Set<b.Majoraw>().ToArray();
                    foreach (var rp in rps)
                    {
                        foreach (var ir in rp.Minors)
                        {
                            var mi = mis.FirstOrDefault(i => i.Mid == ir.Imx);
                            if (mi != null) ir.Parentitle = mi.Name;
                        }
                        foreach (var jr in rp.Majors)
                        {
                            var mj = mjs.FirstOrDefault(j => j.Mid == jr.Jmx);
                            if (mj != null) jr.Parentitle = mj.Name;
                        }
                    }
                    return rps.ToList();
                }
                else return p_db.Set<b.Recipe>().Include(r => r.Steps).Where(r => r.Categ == (byte)ct).AsNoTracking().ToList();
            }
        }
    }
    // 保健
    public sealed class HealtholdDog : b.Accesshort<b.Healthold>
    {
        public HealtholdDog()
        { }
        public List<b.Healthold> GetHealtholds(bool withChildren)
        {
            if (withChildren)
            {
                return p_db.Set<b.Healthold>().Include(h => h.Rawithes).ToList();
            }
            else return p_db.Set<b.Healthold>().ToList();
        }
    }
    public sealed class SteplanDog : b.Accesshort<b.Steplan>
    {
        public SteplanDog()
        { }
    }
    public sealed class MajorecipeDog : b.Accesshort<b.Majorecipe>
    {
        public MajorecipeDog()
        { }
    }
    public sealed class PairawDog : b.Accesshort<b.Pairaw>
    {
        public PairawDog()
        { }
    }
    public sealed class RawithealthDog : b.Accesshort<b.Rawithealth>
    {
        public RawithealthDog()
        { }
    }
    public sealed class MinorecipeDog : b.Accesshort<b.Minorecipe>
    {
        public MinorecipeDog()
        { }
    }
}